package org.thotheolh.sweatkeeper.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

import org.thotheolh.sweatkeeper.db.SQLManager;

public class Resource {

	private static Resource instance = null;
	private SQLManager sqlMan = null;
	private org.thotheolh.sweatkeeper.ui.Main mainUI = null;
	private ArrayList<Object> methodList = new ArrayList<Object>();
	
	protected Resource() {
        init();
    }
	
	public static Resource getInstance() {
        if (instance == null) {
            instance = new Resource();
        }
        return instance;
    }
	
	public void init(){
		
	}
	
	public void setSql(SQLManager sql){
		sqlMan = sql;
	}
	
	public SQLManager getSql(){
		return sqlMan;
	}
	
	public void setMainUI(org.thotheolh.sweatkeeper.ui.Main UI){
		mainUI = UI;
	}
	
	public org.thotheolh.sweatkeeper.ui.Main getMainUI(){
		return mainUI;
	}
	
	public void registerRefreshable(Object c){
		methodList.add(c);
	}
	
	public void callRefreshable(){
		for (int i = 0; i < methodList.size(); i++){
			Object tmp = methodList.get(i);
			Method m;
			try {
				m = tmp.getClass().getDeclaredMethod("initData", null);
				m.invoke(tmp, null);
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
	
}
